import 'dart:convert';
import 'dart:io';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:provider/provider.dart';
import 'package:xiongan_app_yy/entrys/rp/erp_odd_numbers_entity.dart';
import 'package:xiongan_app_yy/entrys/rp/inv_list_details_entity.dart';
import 'package:xiongan_app_yy/entrys/rp/return_booking_entity.dart';
import 'package:xiongan_app_yy/net/internet_api.dart';
import 'package:xiongan_app_yy/provider/erp_info.dart';
import 'package:xiongan_app_yy/utils/image_utils.dart';

import 'comm/count_button.dart';
import 'comm/count_button_return.dart';

class InvListWidget extends StatefulWidget {
  final BuildContext context;
  final int index;
  final String title;
  final String type;
  ErpOddNumbersDatasDetail data;
  InvListDetailsDatasTaskdetaillist dataDetail;
  ReturnBookingOutorderdetaillist rDetail;
  //final ErpOddNumbersDatasDetail data;

  //InvListWidget({this.context, this.index, this.data});
  InvListWidget({this.context, this.index, this.title, this.type, this.data,this.dataDetail,this.rDetail});

  @override
  State<StatefulWidget> createState() {
    return InvListWidgetState(index);
  }
}

class InvListWidgetState extends State<InvListWidget> {
  int i;

  InvListWidgetState(this.i);

  String image = "assets/images/build.png";
  Widget imageWidget;
  String mMaterialno;
  String mModel;
  String mSpec;
  String mName = "";
  int mQuantity;
  String mBaseunitname;
  ErpOddNumbersDatasDetail _data;
  InvListDetailsDatasTaskdetaillist dataDetail;
  ReturnBookingOutorderdetaillist rDetail;

  bool visibility = false;

  @override
  Widget build(BuildContext context) {
    if (widget.type == "push") {
      //出入库提交明细
      if (widget.title == "结余退库物资明细") {
        //特殊的入库提交明细
        rDetail = widget.rDetail;
        if (rDetail.imageurl != null && image == "assets/images/build.png") {
          ImageDownloadUtil.getImageBase64(rDetail.imageurl, (data) {
            setState(() {
              image = data;
            });
          });
        }
        mMaterialno = rDetail.materialcode == null ? "" : rDetail.materialcode;
        mName = rDetail.materialname ?? "";
        mModel = rDetail.model == null ? "" : rDetail.model;
        mSpec = rDetail.spec == null ? "" : rDetail.spec;
        mQuantity = rDetail.quantity;
        mBaseunitname =
            rDetail.baseunitname == null ? "" : rDetail.baseunitname;
      } else {
        _data = widget.data;
        if (_data.imageurl != null && image == "assets/images/build.png") {
          ImageDownloadUtil.getImageBase64(_data.imageurl, (data) {
            setState(() {
              image = data;
            });
          });
        }
        mMaterialno = _data.materialcode == null ? "" : _data.materialcode;
        mName = _data.materialname ?? "";
        mModel = _data.model == null ? "" : _data.model;
        mSpec = _data.spec == null ? "" : _data.spec;
        mQuantity = _data.quantity;
        mBaseunitname = _data.baseunitname == null ? "" : _data.baseunitname;
      }
    } else if (widget.type == "pull") {
      //出入库提交后获取详情明细
      if (widget.title == "结余退库物资明细") {
        //特殊的入库提交后获取详情明细
        dataDetail = widget.dataDetail;
        if (dataDetail.imageurl != null && image == "assets/images/build.png") {
          ImageDownloadUtil.getImageBase64(dataDetail.imageurl, (data) {
            setState(() {
              image = data;
            });
          });
        }
        mName = dataDetail.materialname ?? "";
        mMaterialno =
            dataDetail.materialcode == null ? "" : dataDetail.materialcode;
        mModel = dataDetail.model == null ? "" : dataDetail.model;
        mSpec = dataDetail.spec == null ? "" : dataDetail.spec;
        mQuantity = dataDetail.qty == null ? 0 : dataDetail.qty;
        mBaseunitname =
            dataDetail.baseunitname == null ? "" : dataDetail.baseunitname;
      } else {
        dataDetail = widget.dataDetail;
        if (dataDetail.imageurl != null && image == "assets/images/build.png") {
          ImageDownloadUtil.getImageBase64(dataDetail.imageurl, (data) {
            setState(() {
              image = data;
            });
          });
        }
        mName = dataDetail.materialname ?? "";
        mMaterialno =
            dataDetail.materialcode == null ? "" : dataDetail.materialcode;
        mModel = dataDetail.model == null ? "" : dataDetail.model;
        mSpec = dataDetail.spec == null ? "" : dataDetail.spec;
        mQuantity = dataDetail.qty == null ? 0 : dataDetail.qty;
        mBaseunitname =
            dataDetail.baseunitname == null ? "" : dataDetail.baseunitname;
      }
    }
    imageWidget = ImageDownloadUtil.isImageType(image);
    visibility = isTitle();
    return Container(
      color: Colors.white,
      width: double.infinity,
      padding: EdgeInsets.all(15),
      margin: EdgeInsets.only(top: 10),
      child: Column(
        children: <Widget>[
          Row(
            children: <Widget>[
              Container(
                child: imageWidget,
                width: 80,
                height: 80,
                margin: EdgeInsets.only(right: 10),
              ),
              Expanded(
                  child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  Text(mName,
                      style: TextStyle(fontSize: 13, color: Color(0xff646464))),
                  Text("实物ID：$mMaterialno",
                      style: TextStyle(fontSize: 13, color: Color(0xff646464))),
                  Row(
                    children: <Widget>[
                      Expanded(
                        child: Column(children: <Widget>[
                          Text("型号：$mModel",
                              style: TextStyle(
                                  fontSize: 13, color: Color(0xff646464))),
                          Text("数量：$mQuantity",
                              style: TextStyle(
                                  fontSize: 13, color: Color(0xff646464)))
                        ], crossAxisAlignment: CrossAxisAlignment.start),
                        flex: 1,
                      ),
                      Expanded(
                        child: Column(children: <Widget>[
                          Text("规格：$mSpec",
                              style: TextStyle(
                                  fontSize: 13, color: Color(0xff646464))),
                          Text("基本单位：$mBaseunitname",
                              style: TextStyle(
                                  fontSize: 13, color: Color(0xff646464)))
                        ], crossAxisAlignment: CrossAxisAlignment.start),
                        flex: 1,
                      )
                    ],
                  ),
                ],
              ))
            ],
          ),
          Offstage(
              offstage: visibility,
              child: Container(
                  padding: EdgeInsets.only(top: 10),
                  child: Row(
                    children: <Widget>[
                      Expanded(
                          child: Text("实际数量：",
                              style: TextStyle(
                                  fontSize: 13, color: Color(0xff646464)))),
                      getWid()
                      //
                    ],
                  ))),
        ],
      ),
    );
  }

  Widget getWid() {
    if (widget.type == "push") {
      if (widget.title == "结余退库物资明细") {
        return CountButtonReturn(rDetail, i,Provider.of<ErpInfo>(context).rbean.outOrderDetailList[i]);
      } else {
        return CountButton(_data, i,Provider.of<ErpInfo>(context).mode.details[i]);
      }
    } else {
      return Container();
    }
  }

  bool isTitle() {
    if (widget.title == "出库物资明细") {
      return true;
    } else {
      if (widget.type == "push") {
        return false;
      } else {
        return true;
      }
    }
  }
}
